﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameControl : MonoBehaviour
{
    public static GameControl _Instance;
    private void Awake()
    {
        _Instance = this;
    }

    public GameObject Player;

    public Transform[] Createpos;

    float timervalue = 0;

    int ljnum = 0;
    //public GameObject[] Enemymodle = new GameObject[4];
    List<GameObject> enemylist = new List<GameObject>();

    private void Start()
    {
        DataInit();
        CreateEnemy();
    }

    private void Update()
    {
        timervalue -= Time.deltaTime;
        if(timervalue <= 0)
        {
            timervalue = 0;
            ljnum = 0;
        }
    }

    public void PlayerAttack()
    {
        BattleScenelui._instance.iscd = true;
        Player.GetComponent<PlayerControl2>().PlayerAttack();
        ljnum++;
        timervalue = 2;
        BattleScenelui._instance.AddScore();
        BattleScenelui._instance.Reducekill();
        StartCoroutine(Showlj());
    }

    private void DataInit()
    {
        Const.isgameover = false;
        for (int i = 0; i < VariableDocument.GetInstance.Enemypath.Length; i++)
        {
            GameObject go = DataManager.GetInstance.Loadasset(ResourcesEnum.Prefab, VariableDocument.GetInstance.Enemypath[i]);
            enemylist.Add(go);
        }
    }

    public void CreateEnemy()
    {
        int id = Random.Range(0, 4);
        int index = Random.Range(0,4);
        GameObject temp = Instantiate(enemylist[id], Createpos[index],false);
        
        //temp.transform.SetParent(Createpos[index]);
        StartCoroutine(TimeDelay());
    }

    IEnumerator TimeDelay()
    {
        yield return new WaitForSeconds(1f);
        if (!Const.isgameover)
        {
            CreateEnemy();
        }
    }
    IEnumerator Showlj()
    {
        yield return new WaitForSeconds(0.35f);
        BattleScenelui._instance.CreateLj(ljnum);
    }


}


